<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证码</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        span {
            width: 40px;
            height: 25px;
            line-height: 25px;
            background-color: aqua;
            font-size: 25px;
            -webkit-user-select: none;
        }
    </style>
</head>

<body>
    <form action="">
        <label for="tex">验证码:</label>
        <input type="text" id="tex">
        <span></span>
        <input type="button" value="提交" id="submit">
        <p></p>
    </form>
</body>
<script src="../js/public.js"></script>
    <script>
        (function () {
            let span = document.querySelector('span');
            let tex = document.getElementById('tex');
            let submit = document.getElementById('submit');
            let p = document.querySelector('p');

            auth();

            span.onclick = auth;

            function auth() {
                let number = '';
                for (let i = 0; i < 4; i++) {
                    number += randomNum(1, 9);
                }
                span.innerHTML = number;
            }

            submit.onclick = function () {
                if (tex.value == span.innerHTML) {
                    p.innerHTML = '验证码正确';
                    p.style.color = 'green';
                }else {
                    p.innerHTML = '验证码错误,请重新输入验证码';
                    p.style.color = 'red';
                }
            }






        })();
    </script>
</html>